package com.zhang.genericdemo.actuator;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.zhang.genericdemo.request.BaseRequest;
import com.zhang.genericdemo.response.BaseResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @Author: ZhangGongMing
 * @CreateTime: 2025/4/25 09:30
 * @Description:
 * @Version: 1.0
 */
@Service
@AllArgsConstructor
public class Client {

    public <T extends BaseResponse> T execute(BaseRequest<T> req) {
        String result = this.postRequest(JSONUtil.toJsonStr(req));
        return JSONUtil.toBean(result, req.getResponseType());
    }

    /**
     * 内部所有接口都要是 POST 请求
     *
     * @param param
     * @return
     */
    private String postRequest(String param) {
        return HttpUtil.createPost("http://localhost:8080/api/v1").body(param).execute().body();
    }
}
